/**
 * Created by CN-xin on 2018/10/22999.
 */
// var  modelActi  = localStorage.getItem('sqlData');
// console.log(modelActi);
mini.parse();

var url = bootPATH + "../frame/frame1/pages/activiti/";      //路径前缀

var nowTabObj = parent.mainTab();     //调用index的mainTab方法接受传来的数据
console.log(nowTabObj);
// nowTabObj=JSON.parse(nowTabObj);
var row1=nowTabObj.row;    //打点出来row
var row=JSON.parse(row1);   //转化成JSON，然后就成了全局变量可以任意调用
console.log(row);
mini_debugger = false;
var grid = mini.get("datagrid1");
//页面初始化
$(listDetai);
// 获取表单是否存在

/*加载数据*/
function listDetai(){
	httpRequest();
}

/*查看流程图*/
/*
function sercherPng() {
    var row = grid.getSelected();
    console.log(row)
    var deploymentId=row.deploymentId
    var diagramResourceName=row.diagramResourceName
    fileUrl = ajaxUrl + "/activitiFormController/showView.do?deploymentId="+deploymentId+"&diagramResourceName="+diagramResourceName;
    images = document.getElementById("imgs")
    images.src = fileUrl;
    var process = document.getElementById("process");
    console.log(process);
    process.style.display = "block";
}
*/

/*关闭*/
function close(){
    images = document.getElementById("imgs")
    images.src = '';
    var process = document.getElementById("process");
    console.log(process);
    process.style.display = "none";
}

/*跳转新增页面*/
function Toadd(){
    var table = document.getElementById("add")
    table.style.display = "block";
}

/*创建*/
function add(){
    console.log("row",row);
    console.log("创建流程row",row);
    var modelId = row.id;

    console.log("模型id",modelId);
    var process = mini.get("processName");
    var processName = process.getValue();
    var modelId = row.id;
    console.log("模型id",modelId);
    $.ajax({
        url:ajaxUrl + "/process/create.do",
        type:"POST",
        data:{
            processName:processName,
            modelId:modelId
        },
        success:function(data){
        	console.log(data)
            var process = document.getElementById("processName")
            process.value = "";
            var id = data.modelId;
            window.location.href = ajaxUrl + "/modeler.html?modelId="+id;
        },
        error:function(data){
            console.log("失败");
        }
    })
    var table = document.getElementById("add")
    table.style.display = "none";
}

/*取消*/
function out(){
    var table = document.getElementById("add")
    table.style.display = "none";
}

/*删除模型*/
function remove(){
    var row = grid.getSelected();
    if(row==null){
        mini.alert("请选择要删除的模型！！！")
    }else{
    var id=row.id
    if(confirm("是否删除")){
        $.ajax({
            url:ajaxUrl+"/process/remove.do?modelId="+id,
            type:"POST",
            success:function(data){
                 listDetai();
            },
            error:function(data){
                 listDetai();
            }
        })
    }
    }
}

/*编辑*/
function ToModel() {
    console.log("编辑流程")
    var row = grid.getSelected();
    if (row == null) {
        mini.alert("请选择要编辑的模型！！！")
    } else {
    	console.log(row)
        var id = row.id
        window.location.href = ajaxUrl + "/modeler.html?modelId="+id;
    }
}

/*部署*/
function deploy(){
    // var rowString = localStorage.getItem("sqlData");
    // console.log("rowString",rowString);
    // var row1 = JSON.parse(rowString);
    // console.log("row1",row1)
    var row1 = grid.getSelected();
    if (row1 == null) {
        mini.alert("请选择要部署的模型！！！")
    } else {
        var formId = row.id
        var modelId = row1.id
        console.log(modelId)
        $.ajax({
            url:ajaxUrl + "/activitiFormController/deploy.do?modelId="+modelId+"&formId="+formId,
            type:"POST",
            success:function(data){
            	console.log(data);
               if(data.type=="success"){
                   mini.alert(data.msg);
               }else{
                   mini.alert(data.msg);
               }
            },
            error:function(data){
                mini.alert("部署失败")
                listDetai();
            }

        })

    }
}
// function httpRequest(){
// 	$.ajax({
//         url: ajaxUrl+ "/model/findByProcessId.do",
//         type:"POST",
//         data: {
//             modelId: ''
//         },
//         success : function(data){
//             console.log(data);
//             var activitiList = data.data;
//             console.log(activitiList);
//             grid.setData(activitiList);
//         },
//         error : function(data){
//             console.log("失败")
//         }
//     })
// }
//刷新
// function refresh(){
// 	httpRequest();
// }
// function httpRequest(){
// 	$.ajax({
//         url: ajaxUrl+ "/process/modelList.do",
//         type:"POST",
//
//         success : function(data){
//             console.log(data);
//             var activitiList = data.data;
//             console.log(activitiList);
//             grid.setData(activitiList);
//         },
//         error : function(data){
//             console.log("失败")
//         }
//     })
// }
// //刷新
// function refresh(){
// 	httpRequest();
// }
function httpRequest(){
    // var rowString = localStorage.getItem("sqlData");
    // console.log("rowString",rowString);
    // row = JSON.parse(rowString);
    // console.log("row",row)
    $.ajax({
        // url: "http://localhost:8080/DSMP/formltems/findByModelId.do?modelId=330",
        url: ajaxUrl + "/model/findByProcessId.do?modelId=" + row.id,
        type: "post",
        dataType: "json",
        contentType : "application/json",
        success: function (data) {
            console.log("data",data);
            if (data.state == 'success'){
                var activitiList = data.data;
                console.log(activitiList);
                grid.setData(activitiList);
            }else {
                mini.alert(data.message);
            }
        },
        error: function (data) {

        }
    });
}
//刷新
function refresh(){
	httpRequest();
}
function onActionRenderer(e) {
    console.log(e);
    var grid = e.sender;
    var record = e.record;
    var uid = record._uid;
    var rowIndex = e.rowIndex;

    var s = ' <a class="mini-button Edit_Button" href="javascript:onSqlDesign(\'' + uid + '\')" >停止</a>'

        + ' <a class="mini-button Edit_Button" href="javascript:onActivityDesign(\'' + uid + '\')" >发布</a>';
    return s;
}
